<?php
require 'Model.php';
class VideoModel extends Model{
	function __Construct(){
		parent::__Construct();
		$this->mod='videos';
	}
	function getList(){
		
			$sql = "select * from {$this->mod}";
			$data = $this->conn->query($sql);
			return $data;
		}
		function add($data)
		{
			if(!empty($_FILES["pic"]["name"])){
				$pic = $this->uploadfile('pic','upload/',array('jpg','gif','png'));
			}
			else{
				$pic='';
			}

			$video = array(

        'videoname' => trim($data['videoname']),  
					'tid' => $data['tid'],
					'address' => $data['address'],
					'intro' => $data['intro'],
					'pic'=>$pic,
					'uploaddate'=>date("YmdHis"),
					'uploadadmin'=>$data['uploadadmin'],
					'hittimes'=>0,
					'downtimes'=>0
			
				);
			$keys = implode(',',array_keys($video));
			$values = implode("','", array_values($video));
			$sql="insert into videos ('{$keys}') VALUES ('{$values}')";
			echo "$sql";
			  //执行SQL语句
			$result = $this->conn->query($sql);
			return $result;
		}
		function getVideoByID($id){
			$sql = "select * from videos where vid=".$id;
			$result = $this->conn->query($sql);
			return $result;
		}
		function update($data)
		{
			$video = array(
'id'=>$data['id'],
'videoname'=>trim($data['videoname']),
'tid'=>$data['tid'],
'intro'=>$data['intro'],
'address'=>$data['address']

				);
			if(!empty($_FILES["pic"]["name"])){
				$pic = $this->uploadfile('pic','upload/',array('jpg','gif','png'));
				$video['pic'] = $pic;
			}
			
			
			$sets = '';
			foreach ($video as $key=>$val){
				$sets.=$key."='".$val."',";
			}
			    $sets=rtrim($sets,','); //去掉SQL里的最后一个逗号
			    
			    $sql="UPDATE videos SET {$sets} where vid=".$data['id'];
			  //执行SQL语句
			    $result = $this->conn->query($sql);
			    return $result;		    
			}
			function delete($id)
			{
				$sql = "delete from videos where vid=".$id;
				$result = $this->conn->query($sql);
				return $result;
			}




		}
		?>